//
//  AXFGoodsCell.m
//  BEEQUICK
//
//  Created by Joker on 2016/11/21.
//  Copyright © 2016年 SevenGroup. All rights reserved.
//

#import "AXFGoodsCell.h"

#import <UIImageView+WebCache.h>
#import "AXFFootView.h"
#import "AXFModelManager.h"
@interface AXFGoodsCell () <UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *goodImage;
@property (weak, nonatomic) IBOutlet UILabel *goodName;
@property (weak, nonatomic) IBOutlet UILabel *goodPrice;
@property (weak, nonatomic) IBOutlet UILabel *goodShopCount;
//对号按钮
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;


@end

@implementation AXFGoodsCell
//设置对号按钮的状态
- (void)changeRightBtn:(UIButton *)btn{

    if (btn.selected) {
        self.rightBtn.selected = YES;
    }else{
        self.rightBtn.selected = NO;
    }
    
    if (self.rightBtn.selected) {
        self.model.btnSelect = YES;
    }else{
        self.model.btnSelect = NO;
    }


}



//对号按钮
- (IBAction)rightBtn:(UIButton *)sender {
    
    sender.selected = !self.rightBtn.selected;

    
    self.model.btnSelect = sender.selected;
    
    //
    if ([self.delegate respondsToSelector:@selector(AXFGoodsCell:withRightBtn:)]) {
        [self.delegate AXFGoodsCell:self withRightBtn:sender];
    }

    
    
}

// 加号按钮
- (IBAction)addBtn:(UIButton *)sender {
    
    self.model.shopCount ++;
    _goodShopCount.text = @(self.model.shopCount).description;
    
    
    if ([self.delegate respondsToSelector:@selector(addGoodsCellModel)]) {
        [self.delegate addGoodsCellModel];
        
        
    }
    
    
}
//  减号按钮
- (IBAction)minusBtn:(UIButton *)sender {
    self.model.shopCount --;
    _goodShopCount.text = @(self.model.shopCount).description;
    
    
    if ([self.delegate respondsToSelector:@selector(minusesGoodsCellModel)]) {
        [self.delegate minusesGoodsCellModel];
        

        
    }
    
    
    if (self.model.shopCount == 0) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"确定删除商品吗？" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];
    }
}
//   MARK: 1. 点击alert 实现方法 //  取消按钮
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0)
    {
        //        NSLog(@"取消");
        self.model.shopCount++;
        if ([self.delegate respondsToSelector:@selector(minusesGoodsCellModel)]) {
            [self.delegate minusesGoodsCellModel];
        }
        _goodShopCount.text = @(self.model.shopCount).description;
        
    };
    if (buttonIndex == 1) {
        [self deleteGoods:self.model];
    }
}
//  确定按钮
- (void)deleteGoods:(AXFDetailsModel *)model
{
    if ([self.delegate respondsToSelector:@selector(deleteGoodsCellModel:)]) {
        
        [self.delegate deleteGoodsCellModel:model];
    }
}

//赋值
- (void)setModel:(AXFDetailsModel *)model{
    
    _model = model;
    
    //图片
    //图片的url
    NSString * urlStr = model.img;
    //
    //    //删除尾缀
    //
    NSURL * url = [NSURL URLWithString:urlStr];
    
    //请求图片并显示
    [_goodImage sd_setImageWithURL:url];
    
    _goodShopCount.text = @(model.shopCount).description;
    
    //商品名
    _goodName.text = model.name;
    
    //价格
    _goodPrice.text = [NSString stringWithFormat:@"¥ %.2lf",model.partner_price];
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    //选择按钮在加载时默认选中状态
    self.rightBtn.selected = YES;
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
